package com.musifind.wsclient.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.musifind.wsclient.helpers.UserHelper;
import com.musifind.wsclient.objects.Listening;
import com.musifind.wsclient.objects.User;
import com.musifind.wsclient.url.ListeningRestURIConstants;

public class ListeningService extends BaseService {

	public static void createListening(int userId, String trackId, String trackName, String artistName, String image) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

		Listening listening = new Listening();
		User user = UserService.getUser(userId);
		listening.setKullanici(user);
		listening.setTrackId(trackId);
		listening.setTrackName(trackName);
		listening.setArtistName(artistName);
		listening.setImage(image);
		listening.setBaslangicTarih(Calendar.getInstance());
		Listening response = restTemplate.postForObject(SERVER_URI + ListeningRestURIConstants.CREATE_LISTENING,
				listening, Listening.class);
	}

	public static Listening getListening(int id) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		Listening response = restTemplate.getForObject(SERVER_URI + "/listening/" + id, Listening.class);
		return response;
	}

	public static List<Listening> getWhoIsListeningWhat() {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		List<LinkedHashMap> listenings = restTemplate.getForObject(SERVER_URI
				+ ListeningRestURIConstants.GET_WHO_IS_LISTENING_WHAT, List.class);

		List<Listening> dinlemes = new ArrayList<Listening>();
		for (LinkedHashMap map : listenings) {
			Listening listening = new Listening();
			listening.setId(new Integer("" + map.get("id")));
			listening.setKullanici(UserHelper.getUserFromLinkedHashMap((LinkedHashMap) map.get("kullanici")));
			listening.setTrackName((String) map.get("trackName"));
			listening.setTrackId((String) map.get("trackId"));
			listening.setArtistName((String) map.get("artistName"));
			listening.setImage((String) map.get("image"));
			Calendar baslangic = Calendar.getInstance();
			baslangic.setTimeInMillis((Long) map.get("baslangicTarih"));
			listening.setBaslangicTarih(baslangic);
			dinlemes.add(listening);
		}
		return dinlemes;
	}

	public static void stopListening(int id) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		Listening response = restTemplate.getForObject(SERVER_URI + ListeningRestURIConstants.STOP_LISTENING + id,
				Listening.class);
	}

	public List<Listening> getHistory(int kullaniciId) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		List<LinkedHashMap> listenings = restTemplate.getForObject(SERVER_URI + ListeningRestURIConstants.GET_HISTORY
				+ kullaniciId, List.class);

		List<Listening> dinlemes = new ArrayList<Listening>();
		for (LinkedHashMap map : listenings) {
			Listening listening = new Listening();
			listening.setId(new Integer("" + map.get("id")));
			listening.setKullanici(UserHelper.getUserFromLinkedHashMap((LinkedHashMap) map.get("kullanici")));
			listening.setTrackName((String) map.get("trackName"));
			listening.setTrackId((String) map.get("trackId"));
			listening.setArtistName((String) map.get("artistName"));
			listening.setImage((String) map.get("image"));
			Calendar baslangic = Calendar.getInstance();
			baslangic.setTimeInMillis((Long) map.get("baslangicTarih"));
			listening.setBaslangicTarih(baslangic);
			dinlemes.add(listening);
		}
		return dinlemes;
	}
}
